<template>
  <div class="app-container">
    <div class="change">
      <p>
        <span>单据日期</span>
        <el-date-picker v-model="tempData.purchaseTime" :disabled="islook" type="date" value-format="yyyy-MM-dd" class="userselect" placeholder="选择日期"></el-date-picker>
      </p>
			<p>
			  <span>单据号</span>
			  <el-input v-model="tempData.invoices" :disabled="islook" type="text" class="userselect" placeholder="请输入单据号"/>
			</p>
      <!-- <p>
        <span>供应商</span>
        <el-select v-model="tempData.supplierId" placeholder="请选择供应商" class="userselect">
          <el-option
            v-for="item in gongying"
            :key="item.supplierId"
            :label="item.supplierName"
            :value="item.supplierName"
          ></el-option>
        </el-select>
      </p> -->
      <p>
        <span>领料部门</span>
				<el-input type="text" disabled :value="user.departments" class="userselect"></el-input>
        <!-- <el-select v-model="tempData.departments" placeholder="请选择领料部门" class="userselect">
          <el-option
            v-for="item in ifydata"
            :key="item.id"
            :label="item.departments"
            :value="item.departments"
          ></el-option>
        </el-select> -->
      </p>
      <p>
        <span>领料人</span>
				<el-input type="text" disabled :value="user.username" class="userselect"></el-input>
        <!-- <input v-model="tempData.operator" type="text" placeholder="请选择采购员" @focus="checkBuyer" /> -->
      </p>
      <!-- <p>
        <span>采购类型</span>
        <input v-model="tempData.invoices" type="text" placeholder="请输入采购类型" />
      </p> -->
      <!-- <p>
        <span>发运方式</span>
        <input v-model="tempData.transports" type="text" placeholder="请输入发运方式" />
        <el-select v-model="tempData.transports" placeholder="请选择发运方式" class="userselect">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </p> -->
    </div>
    <!-- 产品数据 -->
    <div class="zd3-btm">
      <div class="addDate" @click="addPeople" v-if="!islook">+ 新增产品</div>
      <el-table :data="proData" :show-header="true" style="width: 100%;border-radius: 8px;">
        <el-table-column type="index" label="序号" align="center" width="80" />
				<el-table-column align="center" label="品牌" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.brand" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
        <el-table-column align="center" label="名称" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.commodityName" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="频率" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.frequencys" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="润滑方式" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.lubrication" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="数量" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.num" @input="numChange(scope.row,scope.$index)" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="参数" width="200">
          <template slot-scope="scope">
            <input v-model="scope.row.parameters" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
				<el-table-column align="center" label="单重" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.pieceWeight" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
				<el-table-column align="center" label="功率" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.powers" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
				<el-table-column align="center" label="转/分" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.revolutionsPerMinute" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
				<el-table-column align="center" label="材质" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.texture" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
				<el-table-column align="center" label="单位" width="150">
				  <template slot-scope="scope">
				    <input v-model="scope.row.nuit" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
				  </template>
				</el-table-column>
        <el-table-column align="center" label="单价" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.unitPrice" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="采购金额" width="150">
          <template slot-scope="scope">
            <input v-model="scope.row.unitPrice*scope.row.num" :class="scope.row.ischeck ? 'inps-active' : 'inps'"  type="text" :disabled="!scope.row.ischeck"/>
          </template>
        </el-table-column> -->
        <el-table-column align="center" width="150" label="操作" fixed="right" v-if="!islook">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="save(scope.row,scope.$index)" v-if="scope.row.ischeck">保存</el-button>
            <el-button type="text" size="small" @click="write(scope.row,scope.$index)" v-else>修改</el-button>
            <el-popconfirm
              placement="top"
              hideIcon
              title="是否要删除此行？"
              @onConfirm="deleteRow(scope.row,scope.$index)"
            >
              <el-button type="text" size="small" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
			<div class="submits">
				<div class="save" @click="saveOrder" v-if="!islook">保存</div>
				<div class="nosave" @click="nosaveOrder" v-if="!islook">取消</div>
				<div class="save" @click="back" v-if="islook">返回</div>
			</div>
    </div>
		<!-- 新增信息 -->
		<el-dialog title="新增产品" :visible.sync="addProShow" width="80%">
		  <div class="product">
        <div class="pro-l">
          <el-tree
            :data="twoClassify"
            node-key="name"
            :props="{label: 'name',children:'listwarehouseGoods'}"
            @current-change="handleNodeClick"
          ></el-tree>
        </div>
        <div class="pro-r">
          <div class="zd3-title">
            <p>
              <span>资产名</span>
              <input v-model="propertyName" type="text" placeholder="请输入资产名" />
            </p>
            <el-button size="small" type="success" @click="searchList">查询</el-button>
            <el-button size="small" @click="resetList">重置</el-button>
          </div>
          <el-table
            ref="singleTable"
            :data="proDeail"
            highlight-current-row
            @current-change="handleAdd"
            style="width: 100%"
          >
            <el-table-column align="center" prop="brand" label="品牌"/>
            <el-table-column align="center" prop="commodityName" label="名称"/>
            <el-table-column align="center" prop="frequencys" label="频率"/>
            <el-table-column align="center" prop="lubrication" label="润滑方式" />
            <el-table-column align="center" prop="num" label="数量" />
            <el-table-column align="center" prop="parameters" label="参数" />
            <el-table-column align="center" prop="pieceWeight" label="单重" />
            <el-table-column align="center" prop="powers" label="功率" />
            <el-table-column align="center" prop="revolutionsPerMinute" label="转/分" />
            <el-table-column align="center" prop="texture" label="材质" />
            <el-table-column align="center" prop="nuit" label="计量单位" />
            <!-- <el-table-column align="center" prop="number" label="现存量"/> -->
            <el-table-column align="center" prop="unitPrice" label="单价" />
          </el-table>
        </div>
		  </div>
		</el-dialog>
  </div>
</template>
<script>
import { getUser,getBumenNew } from '@/api/people'
import { getGongying,addCaigou,updateCaigouOrPro } from '@/api/caigou'
import { getCangku,productOneClass,withIdGetPro } from '@/api/cangku'
export default {
  data() {
    return {
      radio: "1",
			total: 1,
      value: '',
      tableData: [],
      oneClassify: [],   //一级分类
      twoClassify: [],
			user: '',  //用户信息
      tempData: {
        departments: "",  //部门
        invoices: "",    //发票
        operator: "",    //操作人员
        status: '1',
        purchaseTime: '',   //订单时间
        supplierId: '',     //供应商
        //purchasetype: '1',   //采购类型
        // companyName: "",
      },
			options: [{
				label: '选项1',
				value: 1
			},
			{
				label: '选项2',
				value: 2
      }],
      ifydata: [{
        id: 0,
        departments: '全部',
        value: ''
			}],   //部门列表
      bumen: '',     //部门值
      buyerList: {},
      gongying: [], //供应商管理
      gys:'', //供应商选择值
			proDeail: [],  //商品列表
      peopleData: [],  //人员列表
      currentRow: null,
      gyShow: false, //供应商弹窗状态
      buyerShow: false, //采购员弹窗状态
			addProShow: false, //商品信息弹窗
      proData: [],
      writOradd: 0,//0新增，1编辑
			islook: false
    };
  },
  mounted(){
    // this.getpeople()
    this.cangkuList()
    // this.bumenList()
    // this.gongyingList()
		this.user = this.$store.state.user.userInfo[0]
    console.log(this.$route.params)
		if(this.$route.params.look){
			this.islook = true
		}
    if(this.$route.params.options){
      this.tempData = JSON.parse(this.$route.params.options)
      console.trace('tempData:',this.tempData)
      this.proData = this.tempData.listsCommodity
      this.writOradd = 1
    }else{
      this.writOradd = 0
    }
  },
  methods: {
    // 人员列表
    getpeople(id){
      console.log('id:',id)
      getUser({departments: id}).then(res => {
        console.log(res)
        this.peopleData = res.data.list
      })
    },
    // 部门列表
    bumenList() {
      getBumenNew({}).then((res) => {
        console.log("----------------");
        console.log(res);
        let temp = res.data.filter(item => item.ids === '456')[0].departmentsList
        console.log(temp)
        temp.map((item) => {
          item.value = item.departments;
          return item;
        });
        this.ifydata = temp;
      });
    },
		numChange(res,index){
			console.log(res)
			if(res.num*1 > res.stockNum*1){
				this.$message({
					message: '库存不足！',
					type: 'warning'
				})
				let obj = this.proData[index]
				obj.num = res.stockNum
				this.$set(this.proData, index, obj)
			}
		},
    // 供应商列表
    gongyingList(){
      getGongying({}).then(res => {
        console.log(res)
        this.gongying = res.data
      })
    },
    // 获取商品分类
    // handleOne(index){
    //   console.log(index)
    //   productOneClass({id: index}).then(res => {
    //     console.log(res)
    //     // this.twoClassify = res.data
    //     res.data.map(item => {
    //       this.twoClassify.push({
    //         name: item.depositoryName,
    //         children: [{
    //           name: item.name,
    //           stockSpecs: item.stockSpecs
    //         }]
    //       })
    //     })
    //     console.log(this.twoClassify)
    //   })
    // },
    // 获取商品分类--仓库列表
    cangkuList(){
      getCangku({}).then(res => {
        console.log(res)
        this.twoClassify = res.data
      })
    },
    // 选择人员分类
    handleSelece(index){
			console.log(index)
			console.log('ifydata:',this.ifydata[index])
			const navindex = this.ifydata[index].value ? this.ifydata[index].value : null
			this.getpeople(navindex)
    },
    // 点击分类菜单获取对应商品
    handleNodeClick(data, checked, node) {
      console.log(data)
      if(data.listwarehouseGoods){
        withIdGetPro({pid: data.id}).then(res => {
          console.log(res)
          this.proDeail = res.data
        })
      }else{
        withIdGetPro({gid: data.id}).then(res => {
          console.log(res)
          this.proDeail = res.data
        })
      }
    },
    // 修改商品信息
		write(value,index){
			console.log(index)
			this.proData.map(item => item.ischeck = false)
			let obj = this.proData[index];
			obj.ischeck = true;
			this.$set(this.proData, index, obj);
			// this.proData[index].ischeck = true
			// console.log(this.proData[index].ischeck)
		},
		// 保存修改操作
		save(value,index){
			let obj = this.proData[index];
			obj.ischeck = false;
			this.$set(this.proData, index, obj);
			// this.proData[index].ischeck = false
		},
		// 增加商品
    addPeople() {
      this.addProShow = true
		},
    goPrev(page) {
      console.log(page);
    },
    goNext(page) {
      console.log(page);
    },
		// 确认选择供应商
    handleGongying(val) {
      console.log(val);
      this.currentRow = val.name;
    },
		// 选择供应商
    checkGy() {
      this.gyShow = true;
    },
		// 选择采购员
    checkBuyer() {
      this.buyerShow = true;
    },
		// 选择商品列表增加商品
		handleAdd(row){
			console.log(row)
			if(row.num*1 >= 0){
				row.ischeck = false
				row.stockNum = row.num
				row.num = 1
				this.proData.push({...row})
				console.log(this.proData)
				this.addProShow = false
			}else{
				this.$message({
					message: '库存不足！',
					type: 'warning'
				})
			}
    },
    // 选择操作人员
    handlePeople(row){
      console.log(row)
      this.tempData.operator = row.username
      console.log(this.tempData.buyer)
			this.buyerShow = false
		},
		// 删除对应商品数据
		deleteRow(value,index){
			console.log(index)
			this.proData.splice(index,1)
		},
		// 保存新增入库单
		saveOrder(){
      console.log('保存')
      this.proData.map(item => {
        delete item.updateTime
        delete item.createTime
				delete item.insertTime
				if(!item.warehouseId){
					item.warehouseId = item.id
				}
        return item
      })
      this.tempData.listsCommodity = this.proData
			this.tempData.purchaseType = '领料'
			this.tempData.departments = this.user.departments
			this.tempData.operator = this.user.username
      if(this.writOradd == 0){
				this.tempData.statics = 0
        addCaigou(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1)
        })
      }else{
        updateCaigouOrPro(this.tempData).then(res => {
          console.log(res)
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.go(-1)
        })
      }
      
		},
		// 取消新增
		nosaveOrder(){
			console.log('取消')
			this.$router.go(-1)
		},
		back(){
			this.$router.go(-1)
		}
  }
};
</script>
<style lang="scss" scoped>
.popup {
  padding: 0 20px;
}
.product{
  display: flex;
  .pro-l{
    width: 200px;
  }
  .pro-r{
    flex: 1;
    overflow: hidden;
    /deep/.el-table--enable-row-hover .el-table__body tr:hover>td {
      background-color: rgba(24,160,94,.2);
    }
    /deep/.el-table tr{
      cursor: pointer;
    }
    /deep/.el-table__body tr.current-row>td {
      background-color: rgba(24,160,94,.4);
    }
  }
}
.files {
  display: flex;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  margin-top: 20px;
  overflow: hidden;
  padding: 20px;
}
.files-btm .btn-prev:after {
  content: "上一页";
}
.files-btm .btn-next:before {
  content: "下一页";
}
.files-btm .btn-prev,
.files-btm .btn-next {
  width: 65px;
}
.files-left {
	width: 160px;
	margin-right: 20px;
	/deep/.el-menu{
		border-right: none;
	}
	/deep/.el-menu-item{
		height: 35px;
		line-height: 35px;
		margin-bottom: 8px;
	}
	/deep/.el-menu-item.is-active{
		color: #fff;
		border-radius: 5px;
		background: rgba(24,160,94,.8);
	}
}
.files-right {
  flex: 1;
  overflow: hidden;
}
.files-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  // margin-top: 20px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 45px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.avatar{
	width: 30px;
	height: 30px;
}
.zd3-btm {
  background: rgba(255, 255, 255, 1);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  margin-top: 10px;
  overflow: hidden;
  &-title {
    display: flex;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    background: #ffffff;
    > p {
      text-align: center;
      font-size: 16px;
      color: #333;
    }
  }
  .addDate {
    margin: 20px 32px 10px 32px;
    height: 32px;
    border-radius: 3px;
    text-align: center;
    line-height: 32px;
    font-size: 14px;
    color: #333;
    cursor: pointer;
    border: 1px dashed rgba(0, 0, 0, 0.3);
  }
  .el-button--text {
    margin: 0 15px;
  }
}
.inps {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: none;
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.inps-active {
  width: 95%;
  height: 32px;
  line-height: 32px;
  padding-left: 10px;
  background: rgba(24,160,94,.2);
  border: none;
  color: rgba(0, 0, 0, 0.8);
}
.change {
  display: flex;
  flex-wrap: wrap;
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  > p {
    width: 25%;
    font-size: 14px;
    display: flex;
    align-items: center;
    color: #333;
    /deep/.el-input__inner {
      // width: inherit;
      background: none;
      height: 32px;
      // line-height: 32px;
      color: #333;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding: 0 25px;
    }
    /deep/.el-input__icon {
      line-height: 32px;
    }
    .userselect {
      // max-width: 250px;
      flex: 1;
    }

    > span {
      display: block;
      width: 120px;
      text-align: right;
      padding-right: 15px;
    }
    > input {
      width: inherit;
      flex: 1;
      height: 32px;
      border-radius: 3px;
      border: 1px solid rgba(0, 0, 0, 0.1);
      padding-left: 10px;
      color: #333;
    }
  }
}
.submits{
	display: flex;
	padding: 15px 20px;
	justify-content: flex-end;
	align-items: center;
	>div{
		width: 80px;
		height: 32px;
		text-align: center;
		line-height: 32px;
		color: #fff;
		font-size: 14px;
		border-radius: 4px;
		margin-left: 30px;
		cursor: pointer;
	}
	// .back{
	// 	background: ;
	// }
	.save{
		background: #18A05E;
	}
	.nosave{
		background: #999;
	}
}
@media screen and (max-width: 1400px) {
  .change > p {
    width: 33%;
  }
}
</style>